package com.toKnow.controller;

import com.toKnow.model.entity.Article;
import com.toKnow.model.entity.Column;
import com.toKnow.model.entity.Source;
import com.toKnow.model.vo.*;
import com.toKnow.service.ArticleService;
import com.toKnow.service.ColumnService;
import com.toKnow.service.SourceService;
import com.toKnow.utils.JwtUserSessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/api")
public class SearchController {
    @Autowired
    ArticleService articleService;
    @Autowired
    ColumnService columnService;
    @Autowired
    SourceService sourceService;

    public Integer getUid(HttpSession session){
        try{
            String token = (String) session.getAttribute("userToken");
            return Integer.parseInt(JwtUserSessionUtil.getInfo(token,"id"));
        } catch (Exception e){
            return null;
        }
    }

    @RequestMapping(value = "/search",method = RequestMethod.POST)
    @ResponseBody
    public BasicVo search(HttpSession session,RestrictionVo restrictionVo) {
        try{
            if (restrictionVo.getCategory()==null) return new BasicVo(false,"没有携带类型参数",null);
            Integer uid=getUid(session);
            switch (restrictionVo.getCategory()) {
                case "article":
                    List<Article> articles = articleService.searchArticle(uid,restrictionVo);
                    List<ArticleAbstractVo> articleAbstractVos=new ArrayList<>();
                    for (Article article:articles){
                        articleAbstractVos.add(new ArticleAbstractVo(article));
                    }
                    return new BasicVo(true,null,articleAbstractVos);
                case "source":
                    List<Source> sources = sourceService.searchSource(uid,restrictionVo);
                    List<SourceAbstractVo> sourceAbstractVos=new ArrayList<>();
                    for (Source source : sources){
                        sourceAbstractVos.add(new SourceAbstractVo(source));
                    }
                    return new BasicVo(true,null,sourceAbstractVos);
                case "column":
                    List<Column> columns = columnService.searchColumn(uid,restrictionVo);
                    List<ColumnVo> columnVos=new ArrayList<>();
                    for (Column column : columns){
                        columnVos.add(new ColumnVo(column));
                    }
                    return new BasicVo(true,null,columnVos);
                default:
                    return new BasicVo(false,"非法的类型",null);
            }
        }catch (Exception e){
            return new BasicVo(false,"未知错误",null);
        }
    }
}
